package com.fz.us.oa.web.controller.main.dict;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.dict.entity.Dict;
import com.fz.us.admin.dict.service.DictService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 控制器 - Dict
 * ============================================================================
 * 版权所有 2016 。
 *
 * @version 1.0 2016-08-03 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/dict")
public class DictController extends BaseController {

    private static final long serialVersionUID = -7455602504462044709L;

    @Resource
    private DictService dictService;

    /**
     * 新建页面
     */
    @RequestMapping(value = "create", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String create(@CurrentUser User user) {

        return "/main/dict/dict_input";
    }

    /**
     * 编辑页面
     */
    @RequestMapping(value = "update/{keyId}", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String update(@CurrentUser User user, Model model,
                         @PathVariable String keyId) {

        model.addAttribute("id", keyId);
        return "/main/dict/dict_input";
    }

    /**
     * 保存操作
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Object save(@CurrentUser User user,
                       @RequestParam(required = false) String keyId,
                       String name,
                       String value,
                       @RequestParam(required = false) String description,
                       @RequestParam(required = false) String ifKey) {
        if (StringUtils.isBlank(name)) {
            return resultService.fail("字典名称不能为空");
        }
        if (StringUtils.isBlank(value)) {
            return resultService.fail("关键字不能为空");
        }

        if (StringUtils.isBlank(keyId)) {// 保存
            return ajaxJson(dictService.addDict(name, value, description, StringUtils.equals("ifKey", ifKey)?1:0));
        } else {// 修改
            return ajaxJson(dictService.editDict(keyId, name, value, description, StringUtils.equals("ifKey", ifKey)?1:0));
        }
    }

    /**
     * 初始化表单
     */
    @RequestMapping(value = "init", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object init(@CurrentUser User user) {
        Dict dict = new Dict();

        Map<String, Object> dataMap = dictService.getDetailMap(dict);

        Result result = resultService.successWithData(dataMap);

        return ajaxJson(result);
    }

    /**
     * 获取详情
     */
    @RequestMapping(value = "read/{keyId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object read(@CurrentUser User user,
                       @PathVariable String keyId) {
        Dict dict = dictService.get(keyId);
        if (dict == null){
            return resultService.fail("未找到文档");
        }

        Map<String, Object> dataMap = dictService.getDetailMap(dict);

        Result result = resultService.successWithData(dataMap);

        return ajaxJson(result);
    }

    /**
     * 操作
     */
    @RequestMapping(value = "method/{method}/{keyId}", method = RequestMethod.POST)
    public Object method(@CurrentUser User user,
                         @PathVariable BaseEnumManager.MethodEnum method,
                         @PathVariable String keyId) {
        Result result = dictService.methodEntity(method, keyId);

        return ajaxJson(result);
    }

    /**
     * 列表
     */
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object list(@CurrentUser User user){
        List<Dict> dictList = dictService.listSystem();
        ArrayList<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();

        for (Dict entity : dictList) {
            Map<String, Object> map = dictService.getListMap(entity);
            dataRows.add(map);
        }

        return ajaxJson(resultService.successWithData(dataRows));
    }

}
